package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.runtai.entity.*;
import com.system.runtai.service.CarClassifyService;
import com.system.runtai.service.CarService;
import com.system.runtai.service.PostedCollectService;
import com.system.runtai.service.ProduceCollectService;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 车辆介绍 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "车辆介绍")
@RestController
@RequestMapping("/runtai/car")
public class CarController {

    @Autowired
    private CarService carService;

    @Autowired
    private ProduceCollectService produceCollectService;

    @ApiOperation(value = "所有车辆")
    @GetMapping("/carList")
    public Result<IPage<Car>> carList(@RequestParam(required = false) Integer page,@RequestParam(required = false) String carName,@RequestParam(required = false) Integer limit){

        if (StringUtils.isEmpty(carName)){
            Page<Car> recommendPage = new Page<>(page,limit);

            IPage<Car> iPage = carService.page(recommendPage);
            return Result.ok(iPage);
        }else if (!StringUtils.isEmpty(carName)){
            Page<Car> recommendPage = new Page<>(page,limit);

            LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(Car::getCarname,carName);
            IPage<Car> iPage = carService.page(recommendPage,wrapper);
            return Result.ok(iPage);
        }

        Page<Car> recommendPage = new Page<>(page,limit);

        IPage<Car> iPage = null;
        return Result.fail(iPage).message("未查到数据！！！");
    }

    @ApiOperation(value = "分类车辆")
    @GetMapping("/car/{carclassifyid}")
    public Result<List<Car>> carclassifyByid(@PathVariable Integer carclassifyid){
        LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Car::getCarclassifyid,carclassifyid);
        List<Car> list = carService.list(wrapper);
        return Result.ok(list);
    }

    @ApiOperation(value = "后台车辆详情")
    @GetMapping("/carht/{id}")
    public Result<Car> carByIdht(@PathVariable Integer id){
        Car byId = carService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "所有车辆分类车辆列表")
    @GetMapping("/carListfilter")
    public Result<List<Car>> carListfilter(@RequestParam(required = false) Integer id,@RequestParam(required = false) String carName){

        List<Car> list = null;
        if (id!=null) {
            LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Car::getCarclassifyid, id);
            list = carService.list(wrapper);
            if (carName!=null){
                LambdaQueryWrapper<Car> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(Car::getCarclassifyid, id).like(Car::getCarname,carName);
                list = carService.list(wrapper1);
            }
        }
        return Result.ok(list);
    }

    @ApiOperation(value = "程序车辆详情")
    @GetMapping("/carcx/{id}")
    public Result<Car> carByIdcx(@PathVariable Integer id){

        String s = AuthContextUtil.get();
        Car byId = carService.getById(id);
        LambdaQueryWrapper<ProduceCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProduceCollect::getAppletUserId,Integer.valueOf(s)).eq(ProduceCollect::getCarId,id);
        ProduceCollect one = produceCollectService.getOne(wrapper);



        if (one!=null){
            byId.setCollect(1);

            byId.setNumber(byId.getNumber()+1);
            carService.updateById(byId);
            return Result.ok(byId);
        }else {
            byId.setCollect(0);
            byId.setNumber(byId.getNumber()+1);
            carService.updateById(byId);
            return Result.ok(byId);
        }

    }

    @ApiOperation(value = "添加车辆")
    @PostMapping("/saveCar")
    public Result<Car> saveCar(@RequestBody Car car){

        car.setCreateTime(LocalDateTime.now());
        boolean save = carService.save(car);

        if (save){
            return Result.ok(car).message("添加成功");
        }else {
            return Result.ok(car).message("添加失败");
        }
    }

    @ApiOperation(value = "添加车辆收藏")
    @PostMapping("/collectCar")
    public Result<ProduceCollect> collectCar(@RequestBody Car car){

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<ProduceCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProduceCollect::getAppletUserId,Integer.valueOf(s)).eq(ProduceCollect::getCarId,car.getId());
        ProduceCollect one = produceCollectService.getOne(wrapper);

        if (one!=null){
            return Result.ok(one);
        }else {
            ProduceCollect produceCollect = new ProduceCollect();
            produceCollect.setCreateTime(LocalDateTime.now());
            produceCollect.setCarId(car.getId());
            produceCollect.setAppletUserId(Integer.valueOf(s));
            boolean save = produceCollectService.save(produceCollect);

            if (save){
                return Result.ok(produceCollect).message("添加成功");
            }else {
                return Result.ok(produceCollect).message("添加失败");
            }
        }
    }



    @ApiOperation(value = "修改车辆")
    @PostMapping("/updateCar")
    public Result<Car> updateCar(@RequestBody Car car){

        car.setUpdateTime(LocalDateTime.now());
        boolean update = carService.updateById(car);

        if (update){
            return Result.ok(car).message("修改成功");
        }else {
            return Result.ok(car).message("修改失败");
        }
    }


    @ApiOperation(value = "删除车辆")
    @PostMapping("delectCar/{id}")
    public Result delectCar(@PathVariable Integer id){

        boolean b = carService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "删除车辆收藏")
    @PostMapping("delectCollectCarCollect")
    public Result delectCollectCarCollect(@RequestBody Car car){
        String s = AuthContextUtil.get();

        LambdaQueryWrapper<ProduceCollect> wrapper = new LambdaQueryWrapper<>();
        Integer id = car.getId();
        wrapper.eq(ProduceCollect::getCarId,id).eq(ProduceCollect::getAppletUserId,Integer.valueOf(s));
        boolean b = produceCollectService.remove(wrapper);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询车辆")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<Car>> index4534(@PathVariable Long page, @PathVariable Long limit, Car car){

        Page<Car> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();

        Integer id = car.getId();

        if (id!=null){
            wrapper.eq(Car::getId,id);
        }

        IPage<Car> iPage = carService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }

    @ApiOperation(value = "车辆名称筛选")
    @GetMapping("/filkte/{carname}")
    public Result<List<Car>> filtCarName(@PathVariable String carname){
        LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Car::getNumber, carname);
        List<Car> list = carService.list(wrapper);
        return Result.ok(list);
    }

}

